package com.qiku.controller;

import com.qiku.domain.Course;
import com.qiku.domain.CourseLesson;
import com.qiku.domain.CourseSection;
import com.qiku.domain.ResponseResult;
import com.qiku.service.CourseContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseContent")
public class CourseContentController {
    @Autowired
    private CourseContentService courseContentService;

    /**
     * 根据课程ID查询章节与课时信息
     *
     * @param courseId
     * @return
     */
    @GetMapping("/findSectionAndLesson")
    public ResponseResult findSectionAndLesson(int courseId) {
        ResponseResult result = null;
        List<CourseSection> courseSectionList = courseContentService.findSectionAndLessonByCourseId(courseId);
        if (null != courseSectionList) {
            result = new ResponseResult(true, 200, "响应成功~", courseSectionList);
        } else {
            result = new ResponseResult(false, 500, "查询结果为空！", courseSectionList);
        }
        return result;
    }

    /**
     * 保存和修改章节信息
     *
     * @param section
     * @return
     */
    @PostMapping("/saveOrUpdateSection")
    public ResponseResult saveOrUpdateSection(@RequestBody CourseSection section) {
        ResponseResult result = null;
        try {
            if (null != section.getId() && 0 < section.getId()) {
                courseContentService.updateSection(section);
                result = new ResponseResult(true, 200, "修改成功~", null);
            } else {
                courseContentService.saveSection(section);
                result = new ResponseResult(true, 200, "保存成功~", null);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生异常~", null);
            return result;
        }
    }

    /**
     * 修改章节状态
     *
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateSectionStatus")
    public ResponseResult updateSectionStatus(@RequestParam Integer id, @RequestParam int status) {
        HashMap<String, Integer> map = null;
        ResponseResult result = null;
        try {
            courseContentService.updateSectionStatus(id, status);
            map = new HashMap<>();
            map.put("status", status);
            result = new ResponseResult(true, 200, "修改成功~", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", map);
            return result;
        }
    }

    /**
     * 回显章节对应的课程信息
     * @param courseId
     * @return
     */
    @GetMapping("/findCourseByCourseId")
    public ResponseResult findCourseByCourseId(@RequestParam int courseId) {
        ResponseResult result = null;
        try {
            Course course = courseContentService.findCourseById(courseId);
            Map<String, Object> map = new HashMap<>();
            map.put("id", courseId);
            map.put("status", course.getCourseName());
            result = new ResponseResult(true, 200, "响应成功~", map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", null);
            return result;
        }
    }

    /**
     * 保存课时信息
     * @param courseLesson
     * @return
     */
    @PostMapping("/saveLesson")
    public ResponseResult saveLesson( @RequestBody CourseLesson courseLesson){
        ResponseResult result = null;
        try {
            courseContentService.saveLesson(courseLesson);
            result = new ResponseResult(true, 200, "响应成功~", null);
            return result;
        } catch (Exception e){
            e.printStackTrace();
            result = new ResponseResult(false, 500, "发生错误~", null);
            return result;
        }
    }
}
